package com.antdesignbackend.backend.town.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.antdesignbackend.backend.district.entity.District;
import com.antdesignbackend.backend.entity.Users;
import com.antdesignbackend.backend.service.UserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.town.entity.Town;
import com.antdesignbackend.backend.town.entity.entityVo.TownVo;
import com.antdesignbackend.backend.town.service.TownService;

/**
 * 乡镇管理
 *
 * @author 不二
 * @email jack.com
 * @date 2020-07-21 18:57:57
 */

@Controller
@RequestMapping("/town")
@Slf4j
public class TownController extends BaseController{

	private static final String FUN_NAME = "乡镇管理";//功能名称

	@Autowired
	private TownService townService;
	@Autowired
	private UserService userService;
	@GetMapping("/index")
	@AuthToken
	public String index() {
		return "town/index";
	}


	@PostMapping("/tableSearch")
	@ResponseBody
	ReturnVO tableSearch(@RequestBody TownVo townVo) throws SecurityException, IllegalArgumentException{
		ReturnVO result = new ReturnVO();
		QueryWrapper<TownVo> wrapper = new QueryWrapper<>();
		if (townVo.getCity_id() != null && !townVo.getCity_id().equals("") && !townVo.getCity_id().equals("-1")){
			wrapper.eq("t.city_id",townVo.getCity_id());
		}
		if (townVo.getDistrict_id() != null && !townVo.getDistrict_id().equals("-1") && !townVo.getDistrict_id().equals("")){
			wrapper.eq("t.district_id",townVo.getDistrict_id());
		}
		if (townVo.getTown__name() != null && !townVo.getTown__name().equals("")){
			wrapper.like("town__name",townVo.getTown__name());
		}
		List<TownVo> list = new ArrayList<>();
		int total = townService.count(wrapper);
		if (total > 0){
			list = townService.search(wrapper,new Page<TownVo>(townVo.getPageIndex(),townVo.getPageSize()));
		}
		result.setTotal(total);
		result.setData(list);
		return result;

	}

	@GetMapping("/getById/{town_id}")
	@ResponseBody
	public	Town getById(@PathVariable(name = "town_id") String town_id) {
		Town obj = townService.getById(town_id);
		return obj;
	}

	@PostMapping("/save")
	@ResponseBody
	public ReturnVO save(@RequestBody Town town){
		ReturnVO result = new ReturnVO();
		try {
			if(townService.save(town)){
				result.setMessage("保存成功！");
				result.setCode("200");
			}else{
				result.setMessage("保存失败！");
				result.setCode("500");
			}
		}catch (Exception e){
			log.error(e.getMessage());
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	@PostMapping("update")
	@ResponseBody
	public ReturnVO updateById(@RequestBody Town town){
		ReturnVO result = new ReturnVO();
		if(townService.updateById(town)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	/**
     * 根据id删除
     */
	@DeleteMapping("/deleteById/{id}")
	@ResponseBody
	public ReturnVO deleteById(@PathVariable Serializable id) {
		ReturnVO result = new ReturnVO();
		if (townService.removeById(Integer.parseInt(id.toString())) == true) {
			result.setMessage("删除成功");
		} else {
			result.setMessage("删除失败");
			result.setCode("0");
		}
		return result;
	}

	/**
	 * 根据区县编号查询所有乡镇
	 */
	@GetMapping("/getTownByDistrict")
	@ResponseBody
	public List<Town> getTownByDistrict(@RequestParam(required = false,value = "district_id") String district_id) {
		System.out.println("进入乡镇查询方法");
		QueryWrapper<Town> wrapper = new QueryWrapper<>();
		wrapper.eq("district_id",district_id);
		System.out.println("乡镇数据有"+townService.list(wrapper));
		return townService.list(wrapper);
	}
	/**
	 * 根据session区县编号查询所有乡镇
	 */
	@PostMapping("/getTownByDistrictSession")
	@ResponseBody
	public List<Town> getTownByDistrictSession() {
		System.out.println("进入乡镇查询方法");
		QueryWrapper<Town> wrapper = new QueryWrapper<>();
		Users users=userService.getById(getLoginUserId());
		System.out.println("查询到的用户为"+users);
		Integer district_id=users.getAuthDistrict();
		wrapper.eq("district_id",district_id);
		System.out.println("乡镇数据有"+townService.list(wrapper));
		return townService.list(wrapper);
	}
}
